#!/bin/bash

render_use_default=0

set_render_use_default () {
    render_use_default=$1
}

show_options() {
    tput rc
    local selected_software_support_list="$1_software_support_list[@]"
    local software_support_list=("${!selected_software_support_list}")
    local additional_string="$1_prompt"
    local selected_options="$1_options[@]"
    local options=("${!selected_options}")
    local selected_description="$1_description[@]"
    local description=("${!selected_description}")
    logInfo ""
    eval $additional_string "${#software_support_list[@]}"
    printf "%-10s %-25s %-10s %-16s \n" "Number" "Option" "Choice" "Descriptions"
    printf "%-10s %-25s %-10s %-16s \n" "Number" "Option" "Choice" "Descriptions" >> ${log_file_of_abspath}
    for i in "${!software_support_list[@]}"; do
        if [ "$i" -lt $((${#software_support_list[@]})) ]; then
            local display_name="${software_support_list[i]//_/-}" # "_" -> "-"
            if [ "${options[i]}" == "[v]" ]; then
                printf "%-10s %-25s \033[1;32m%-10s\033[0m %-16s \n" "$((i + 1))" "${display_name}" "${options[i]}" "${description[i]}"
                printf "%-10s %-25s \033[1;32m%-10s\033[0m %-16s \n" "$((i + 1))" "${display_name}" "${options[i]}" "${description[i]}" >> ${log_file_of_abspath}
            else
                printf "%-10s %-25s \033[1;31m%-10s\033[0m %-16s \n" "$((i + 1))" "${display_name}" "${options[i]}" "${description[i]}"
                printf "%-10s %-25s \033[1;31m%-10s\033[0m %-16s \n" "$((i + 1))" "${display_name}" "${options[i]}" "${description[i]}" >> ${log_file_of_abspath}
            fi
        fi
    done
}

user_choose() {
    if [[ $render_use_default -eq 1 ]]; then
        return 0
    fi

    local while_flag=1
    local selected_software_support_list="$1_software_support_list[@]"
    local software_support_list=("${!selected_software_support_list}")
    local selected_options="$1_options[@]"
    local options=("${!selected_options}")
    local choice
    tput sc
    while [[ ${while_flag} == 1 ]]; do
        show_options "$1"
        local toggle_function="toggle_$1_option"
        local num_options=${#software_support_list[@]}
        read choice
        case $choice in
        [1-$num_options])
            eval $toggle_function "$((choice - 1))"
            ;;
        q)
            echo "Exit Installation."
            exit 0
            ;;
        ""|y)
            logSuccess "Confirm Installation."
            while_flag=0
            ;;
        *)
            echo "Invalid Choice, Please Choose again."
            sleep 1
            delete_previous_and_current_line
            ;;
        esac
    done
}

delete_previous_and_current_line() {
    echo -e "\033[F\033[K"
}

prompt_for_yes_no() {
    if [[ $render_use_default -eq 1 ]]; then
        return 0
    fi

    local response
    local prompt=$@
    echo $@
    while true; do
        read -p "" response

        case ${response,,} in
            y|"")  return 0 ;; # 'y' or 'Y' entered, return 0 (success)
            n) return 1 ;; # 'n' or 'N' entered, return 1 (failure)
            *) logWarn "Invalid input. Please enter 'y' or 'n'." ;;
        esac
    done
}

continue_install() {
    if [[ $render_use_default -eq 1 ]]; then
        return 0
    fi

    local choice
    while :
    do
        logInfo "Continue Installation? [y/n] "
        read choice
        case $choice in
        Y|y|"")
            logSuccess "Confirm Installation."
            return 0
            ;;
        N|n)
            echo "Exit Installation."
            exit 0
            ;;
        *)
            echo "Invalid Choice, Please Choose again."
            sleep 1
            delete_previous_and_current_line
            ;;
        esac
    done
}

#clear screen but not clear previous message
clear_screen()
{
    if [[ $render_use_default -eq 1 ]]; then
        return 0
    fi

    term_line=$(tput lines)
    lines=$((term_line))
    printf '\n%.0s' $(seq 1 $lines)
    tput cup 0 0
}